#-*-coding:utf-8-*-

'''
# ----------------------------------------------------------------------------
#  Purpose:     用于实现邮件发送功能函数

#  Author:      青衫少年春归也

#  Created:     2019.12.23

# ----------------------------------------------------------------------------
'''
import json
import requests

import config
from test.test_method  import single_class_method_unittest


class SendEmail():
    @single_class_method_unittest(result=None,
                                  addressee=["yewei8@hikvision.com.cn"], msg_context=["Hello World"], subject="测试",
                                  attachments=["http://minio.hiktest.com:10089/minio/download/tod-otherfile-bucket/tod-e21a74f6-01c8-11e9-862b-005056c00008-设计文档.docx?token=",]
                                 )
    def send_email(self, addressee:list, msg_context:list, subject:str,
                   sender:str="hita", copyto:list=None, template_name:str='tod',
                   attachments:list=None, greet_msg:str="", email_server=None):
        """
        向指定用户发送邮件， 默认通过 邮件服务发送
        参数信息:
            :param addressee:收件人邮箱地址
            :param msg_context:邮件正文信息
                例如：
                    [f"提交人：{username}", f"标题：{title}", f"创建时间：{create_time}",
                     f"描述：{description.replace("\n", "<br>")}",f"状态：{status}"]
            :param subject: 邮件的主题
            :param sender: 发件人，默认以 HITA 邮件进行发送
            :param copyto: 邮件抄送人，默认为空，支持多个抄送对象
            :param template_name:邮件正文模板，默认 TOD 邮件模板
            :param attachments:附件信息，要求附件上传到Minio服务器
            :param greet_msg:问候语，可省略
            :param email_server:邮件服务地址，默认 实验室 邮件服务地址
        """
        server = email_server if email_server else config.EMAIL_SERVER_URL
        attachments = attachments if attachments else []
        copyto = copyto if copyto else []
        data = {
            "sender": sender,
            'subject': subject,
            'to': addressee,
            'cc': copyto,
            'template_name': template_name,
            'template_data': {
                'greet_msg': greet_msg,
                'message_info': msg_context,
                'table_info': None,
            },
            'files': attachments
        }
        headers = {'Content-Type': 'application/json'}
        try:
            requests.post(url=server, data=json.dumps(data), headers=headers)
        except Exception as error:
            raise error

